package org.example.mall.admin.controller.manage;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.example.mall.admin.model.dto.manage.AccountDto;
import org.example.mall.admin.model.entity.manage.Account;
import org.example.mall.admin.model.vo.manage.AccountVo;
import org.example.mall.admin.service.manage.AccountService;
import org.example.mall.admin.util.BusOp;
import org.example.mall.admin.util.R;
import org.example.mall.admin.util.SecurityUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author hzq
 * @since 2021-12-29
 */
@Api(tags = "用户信息")
@RestController
@AllArgsConstructor
@RequestMapping("/manage/account")
public class AccountController {

    private final AccountService accountService;

    @GetMapping("/info")
    @ApiOperation("获取认证信息")
    public R<Map<String, Object>> info() {
        return R.ofSuccess(accountService.info());
    }

    @GetMapping("/baseInfo")
    @ApiOperation("获取基本信息")
    public R<Map<String, Object>> baseInfo() {
        return R.ofSuccess(accountService.baseInfo());
    }

    /**
     * 分页查询员工信息
     *
     * @param page    分页参数
     * @param account 查询参数
     * @return 员工列表
     */
    @GetMapping("/page")
    @ApiOperation("分页查询员工信息")
    public R<IPage<AccountVo>> page(Page page, Account account) {
        return R.ofSuccess(accountService.getPage(page, account));
    }

    @ApiOperation("根据手机号查询用户信息")
    @GetMapping("/phone/{phone}")
    public R<Account> getByPhone(@PathVariable String phone) {
        return R.ofSuccess(accountService.getByPhone(phone));
    }

    @PostMapping
    @ApiOperation("新增员工")
    @PreAuthorize("hasAuthority('account_add')")
    public R<Boolean> add(@RequestBody @Valid AccountDto dto) {
        return R.ofSuccess("添加员工成功",
                BusOp.mustSucceed(accountService.add(dto), "添加员工失败"));
    }

    @PutMapping
    @ApiOperation("修改员工")
    @PreAuthorize("hasAuthority('account_edit')")
    public R<Boolean> edit(@RequestBody @Valid AccountDto dto) {
        return R.ofSuccess("修改员工成功",
                BusOp.mustSucceed(accountService.edit(dto), "修改员工失败"));
    }

    @PutMapping("/selfEdit")
    @ApiOperation("员工自己修改资料")
    public R<Boolean> selfEdit(@RequestBody Account account) {
        return R.ofSuccess("修改资料成功",
                BusOp.mustSucceed(accountService.selfEdit(account), "修改资料失败"));
    }

    @DeleteMapping("/{accountId}")
    @ApiOperation("删除员工")
    @PreAuthorize("hasAuthority('account_del')")
    public R<Boolean> del(@PathVariable Integer accountId) {
        return R.ofSuccess("删除员工成功",
                BusOp.mustSucceed(accountService.del(accountId), "删除员工失败"));
    }

    @PutMapping("/bindEmail")
    @ApiOperation("员工自己绑定/更绑邮箱")
    public R<Boolean> bindEmail(@ApiParam(name = "email", value = "邮箱") @RequestParam String email,
                                @ApiParam(name = "code", value = "邮箱验证码") @RequestParam String code) {
        return R.ofSuccess("绑定邮箱成功",
                BusOp.mustSucceed(accountService.bindEmail(email, code), "绑定邮箱失败"));
    }

    @PutMapping("/editEmail")
    @ApiOperation("管理员修改员工邮箱")
    @PreAuthorize("hasAuthority('account_edit_email')")
    public R<Boolean> editEmail(@ApiParam(name = "email", value = "邮箱") @RequestParam String email,
                                @ApiParam(name = "accountId", value = "员工id") @RequestParam Integer accountId) {
        // 修改邮箱时先判断邮箱是否被使用
        // 邮箱正在被使用是否允许直接修改 即删除该邮箱与原用户的绑定 其他的绑定也是同理
        // 按需处理 我这里处理是只要通过邮箱验证码检验 即删除该邮箱与原用户的绑定 把该邮箱绑定到当前用户上
        return R.ofSuccess("修改邮箱成功",
                BusOp.mustSucceed(accountService.editEmail(accountId, email), "修改邮箱失败"));
    }

    @PutMapping("/bindPhone")
    @ApiOperation("员工自己绑定/更绑手机号")
    public R<Boolean> bindPhone(@ApiParam(name = "phone", value = "邮箱") @RequestParam String phone,
                                @ApiParam(name = "code", value = "手机验证码") @RequestParam String code) {
        return R.ofSuccess("绑定手机号成功",
                BusOp.mustSucceed(accountService.bindPhone(phone, code), "绑定手机号失败"));
    }

    @PutMapping("/editPhone")
    @ApiOperation("管理员修改员工手机号")
    @PreAuthorize("hasAuthority('account_edit_phone')")
    public R<Boolean> editPhone(@ApiParam(name = "phone", value = "邮箱") @RequestParam String phone,
                                @ApiParam(name = "accountId", value = "员工id") @RequestParam Integer accountId) {
        return R.ofSuccess("修改手机号成功",
                BusOp.mustSucceed(accountService.editPhone(accountId, phone), "修改手机号失败"));
    }

    @ApiOperation("管理员修改员工账号密码")
    @PutMapping("/editUsernamePassword")
    @PreAuthorize("hasAuthority('account_edit_username_password')")
    public R<Boolean> editUsernamePassword(@ApiParam(name = "username", value = "用户名") @RequestParam String username,
                                           @ApiParam(name = "password", value = "密码(前端需加密)") @RequestParam String password,
                                           @ApiParam(name = "accountId", value = "员工id") @RequestParam Integer accountId) {
        return R.ofSuccess("修改账号密码成功",
                BusOp.mustSucceed(accountService.editUsernamePassword(accountId, username, password), "修改账号密码失败"));
    }

    @ApiOperation("员工自己修改密码")
    @PutMapping("/editPassword")
    public R<Boolean> editPassword(@ApiParam(name = "originPassword", value = "原密码(前端需加密)") @RequestParam String originPassword,
                                   @ApiParam(name = "newPassword", value = "新密码(前端需加密)") @RequestParam String newPassword) {
        return R.ofSuccess("修改账号密码成功",
                BusOp.mustSucceed(accountService.editPassword(SecurityUtil.getAuth().getAccountId(), originPassword, newPassword), "修改账号密码失败"));
    }

}

